<template>
	<view class="content">
		<view v-if="list.length > 0" class="user-list">
			<block v-for="(item, index) in list" :key="index">
				<view class="user-item">
          <view>
            <checkbox-group @click="choice(item.userID)">
              <label>
                <checkbox  :value="item.userID"  />
              </label>
            </checkbox-group>
            </view>
					<view class="user-avatar">
						<image class="avatar" :src="getAvatarUrl(item.avatar)" mode="aspectFill"></image>
					</view>
					<view class="info-wrap">
						<view class="user-info">
							<view class="name">
								<text>{{item.nick}}</text>
							</view>
							<view class="info">
								<view :class="['sex-wrap', 'sex' + getSex(item.gender)]">
									<image class="sex-icon" :src="'/static/img/sex' + getSex(item.gender) + '.png'" mode=""></image>
								</view>
							</view>
						</view>
					</view>
					<view class="uni-icon uni-icon-arrowright"></view>
				</view>
			</block>
		</view>
		<view v-else class="empty-wrap">
			<image class="empty-icon" src="/static/img/empty.png" mode=""></image>
			<text class="empty-text">暂无好友</text>
		</view>
	</view>
</template>

<script>
	import util from "@/common/util.js"
	export default {
		data() {
			return {
				myInfo: {},
				list: [],
                choiceList:[], // 选择好友的信息
				groupid:'',
			    // 群成员列表
			    memberList:[],
				isdeal:'false',
				types:'',
			}
		},
		onLoad(options) {
			this.groupid=options.groupid;
			try {
				const myInfo = uni.getStorageSync('myInfo');
				if (myInfo) {
					this.myInfo = myInfo;
				}
			} catch (e) {
				console.log(e);
			}
			this.memberList=JSON.parse(options.memberList)
			if(options.types==0){
				this.isdeal='true';
			}
			this.types=options.types;
		},
		onReady() {
			if(this.isdeal=='true'){
				this.list=this.memberList;
				// this.choiceList=this.list.map(item=>{
				// 	return item.userID;
				// })
				uni.setNavigationBarTitle({
				    title: '移除群成员'
				});
			}else{
				this.getFriendList();
			}
			
		},
		// 页面右上角点击事件
		onNavigationBarButtonTap(e){
			console.log('选中用户1111',this.choiceList)
		    if(this.choiceList.length>0){
				var that=this;
			
				if(that.types=='1'){
					that.$tim.addGroupMember({
						groupID: that.groupid+'', 
					    userIDList: that.choiceList,
						}).then(function(imResponse) {
							uni.showToast({
								title: '邀请成功',
								position: 'bottom',
								icon: 'none'
							})
							setTimeout(() => {
								uni.switchTab({
									url:"message"
								})
							}, 1000)
							
						 
					}).catch(function(imError) {
						// 错误信息
						uni.showToast({
							title: imError,
							position: 'bottom',
							icon: 'none'
						})
					 
					});
				}else{
					that.$tim.deleteGroupMember({
						groupID: that.groupid+'', 
					    userIDList:that.choiceList,
					    reason: '你违规了，我要踢你！',
					}).then(function(imResponse) {
					  uni.showToast({
					  	title: '删除成功',
					  	position: 'bottom',
					  	icon: 'none'
					  })
					  setTimeout(() => {
					  	uni.switchTab({
					  		url:"message"
					  	})
					  }, 1000)
					}).catch(function(imError) {
					  uni.showToast({
					  	title: imError,
					  	position: 'bottom',
					  	icon: 'none'
					  })
					});
				}
				
				
		    }else{
				uni.showToast({
					title: '请选择好友',
					position: 'bottom',
					icon: 'none'
				})
			}
		},
		onPullDownRefresh() {
			this.page = 1;
			setTimeout(() => {
				this.getFriendList();
			}, 500)
		},
		methods: {
		    // 选择好友
		    choice(userid){
				
				if(this.choiceList.includes(userid)){
					var indexkey=this.choiceList.indexOf(userid);
					this.choiceList.splice(indexkey,1)
				}else{
					this.choiceList.push(userid)
				}
				
		    },
			
			getFriendList: function () {
				uni.showLoading({
					title: ''
				})
				this.$tim.getFriendList({
					fromAccount: this.IDBEFORE + this.myInfo.id + ''
				}).then(imResponse => {
					
					let friendList = [];
					for (let i = 0; i < imResponse.data.length; i++) {
						friendList.push(imResponse.data[i].userID)
					}
					if (friendList.length > 0) {
						
						this.$tim.getUserProfile({
							userIDList: friendList
						}).then(imResponse => {
							uni.hideLoading();
							uni.stopPullDownRefresh();
							this.list = imResponse.data.map(item=>{
								item.checked='false';
								return item;
							})
                            
						}).catch(imError => {
							uni.hideLoading();
							uni.stopPullDownRefresh();
							
							uni.showToast({
								title: imError,
								position: 'bottom',
								icon: 'none'
							})
						});
					} else {
						uni.hideLoading();
						uni.stopPullDownRefresh();
					}
				}).catch(imError => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
					uni.showToast({
						title: imError,
						position: 'bottom',
						icon: 'none'
					})
				});
			},
			
			getAvatarUrl: function (value) {
				return value.indexOf(this.QINIUURL) == -1 ? value : value + '?imageView2/1/w/100/h/100'
			},
			
			getSex: function (value) {
				let sex = '1';
				if (value == 'Gender_Type_Female') {
					sex = '2';
				}
				return sex;
			},
			// 跳转好友信息
			openUserDetails: function (item) {
				// let user = {
				// 	id: util.getUserID(item.userID, this.IDBEFORE),
				// 	name: item.nick,
				// 	avatar:item.avatar
				// }
				// uni.navigateTo({
				// 	url: '../home/user_home?user=' + JSON.stringify(user)
				// })
			},
		}
	}
</script>

<style lang="scss">
	@import '../../common/common.scss';
	.user-list {
		padding: 1rpx 0;
	}
	.user-item {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		padding: 20rpx 40rpx;
		background-color: #fff;
		.user-avatar {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 1rpx;
			border: 4rpx solid #fff;
			border-radius: 120rpx;
			position: relative;
			.avatar {
				width: 100rpx;
				height: 100rpx;
				border-radius: 100rpx;
				background-color: $image-bg-color;
			}
		}
		.info-wrap {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			flex: 1;
			height: 140rpx;
			border-bottom: 1rpx solid #f9f9f9;
		}
		.user-info {
			flex: 1;
			margin: 0 20rpx;
			.name {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				text {
					max-width: 360rpx;
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					font-size: 36rpx;
					color: #333;
				}
			}
			.info {
				display: flex;
				justify-content: flex-start;
				align-items: center;
			}
			.sex-wrap {
				display: flex;
				justify-content: center;
				align-items: center;
				line-height: 28rpx;
				padding: 5rpx 10rpx;
				border-radius: 28rpx;
				&.sex1 {
					background: #83d7ff;
				}
				&.sex2 {
					background: #fe9ab7;
				}
				.sex-icon {
					width: 28rpx;
					height: 28rpx;
				}
				.age {
					font-size: 28rpx;
					color: #fff;
					margin-left: 5rpx;
				}
			}
			.city-wrap {
				display: flex;
				justify-content: center;
				align-items: center;
				line-height: 28rpx;
				padding: 5rpx 10rpx;
				border-radius: 28rpx;
				background: #4cbcfc;
				margin-left: 10rpx;
				.city-icon {
					width: 28rpx;
					height: 28rpx;
				}
				.city {
					font-size: 28rpx;
					color: #fff;
					margin-left: 5rpx;
				}
			}
			.intro {
				width: 360rpx;
				font-size: 32rpx;
				color: #999999;
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
				margin-top: 10rpx;
			}
		}
		.uni-icon {
			font-size: 28rpx;
			color: #999999;
		}
	}
</style>
